package com.hxzy.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hxzy.util.AdminInfoUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;

/**
 * https://baomidou.com/pages/4c6bcf/
 * mybatis自动填充功能
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {



    /**
     * 新增操作回填
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        this.strictInsertFill(metaObject, "recTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());

      // 新增时间  recTime     ，createTime
//        Arrays.stream(createTimeField).forEach(p-> {
//           if( metaObject.hasSetter(p)){
//               metaObject.setValue(p, new Date());
//           }
//        });
        // userId 当前用户ID
        this.strictInsertFill(metaObject, "userId", Long.class, AdminInfoUtil.getUserId());

        // 当前登录名
        this.strictInsertFill(metaObject,"createBy",String.class,AdminInfoUtil.getAdmin().getUsername());
        this.strictInsertFill(metaObject,"operName",String.class,AdminInfoUtil.getAdmin().getUsername());

    }

    /**
     * 修改操作
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());

        // userId 当前用户ID
        this.strictInsertFill(metaObject, "userId", Long.class, AdminInfoUtil.getUserId());

        // 当前登录名
        this.strictInsertFill(metaObject,"updateBy",String.class,AdminInfoUtil.getAdmin().getUsername());


    }

}
